package com.moleq.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.moleq.base.EditableListener;
import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.OnResultListener;
import com.moleq.dialog.SizeJustifyDialog;
import com.moleq.mgpos.activities.R;
import com.moleq.models.Receipt;

public class ReceiptSetupAdapter extends MQBaseAdapter implements OnResultListener {

	public static final String TRAILER_EDITED = "TrailerEdit";
	public static final String HEADER_EDITED = "HeaderEdit";
	private LayoutInflater mInflater;
	private List<Receipt> listHeader;
	private EditableListener mEditableListener;
	
	private Context shareContext;
	private String ls_size, ls_justify;
	private String mID = "";

	public ReceiptSetupAdapter(Context context, List<Receipt> listHeader) {
		this.mInflater = LayoutInflater.from(context);
		this.listHeader = listHeader;
		this.shareContext = context;
	}

	public void setEditableListener(EditableListener listener) {
		mEditableListener = listener;
	}

	public void setSource(List<Receipt> listHeader) {
		this.listHeader = listHeader;
	}

	public int getCount() {
		return listHeader.size();
	}

	public Object getItem(int position) {
		return listHeader.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		super.getView(position, convertView, parent);
		
		ViewHolder holder = null;
//		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.ps_receipt_row, null);
			holder.tv_header = (TextView) convertView.findViewById(R.id.tv_header);
			holder.iv_set = (ImageView) convertView.findViewById(R.id.iv_set);
			holder.llHeaderRow = (LinearLayout) convertView.findViewById(R.id.llHeaderRow);
			
			holder.iv_set.setOnClickListener(onClickListener);

			convertView.setTag(holder);
//		} else {
//			holder = (ViewHolder) convertView.getTag();
//		}

		holder.tv_header.setTag(position);
		holder.iv_set.setTag(position);

		holder.tv_header.setText(listHeader.get(position).getMsg());

		if (position == selectedRow) {
			holder.llHeaderRow.setBackgroundColor(Color.rgb(255, 219, 16));
		}else {
			holder.llHeaderRow.setBackgroundColor(Color.rgb(204, 204, 204));
		}
		return convertView;
	}

	public OnClickListener onClickListener = new OnClickListener() {

		public void onClick(View view) {

			switch (view.getId()) {
			case R.id.iv_set:
				selectedRow = (Integer) view.getTag();
				ScrollToRow(selectedRow);
				ls_size = listHeader.get(selectedRow).getLarge();
				if (ls_size == null || ls_size.equals("")) {
					ls_size = "";
				} else if (ls_size.equals("L")) {
					ls_size = "Large";
				} else if (ls_size.equals("N")) {
					ls_size = "Normal";
				}
				ls_justify = listHeader.get(selectedRow).getJustify();
				if (ls_justify == null || ls_justify.equals("")) {
					ls_justify = "";
				} else if (ls_justify.equals("L")) {
					ls_justify = "Left";
				} else if (ls_justify.equals("C")) {
					ls_justify = "Center";
				} else if (ls_justify.equals("R")) {
					ls_justify = "Right";
				}

				SizeJustifyDialog mSizeJustifyDialog = new SizeJustifyDialog(shareContext, ReceiptSetupAdapter.this);
				mSizeJustifyDialog.setSource(listHeader, ls_size, ls_justify, selectedRow);
				mSizeJustifyDialog.show();

				break;
			}
		}
	};

	public final class ViewHolder {
		public TextView tv_header;
		public ImageView iv_set;
		public LinearLayout llHeaderRow;
	}

	public void setPage(String id) {
		this.mID = id;
	}

	@Override
	public void onTextChange(int resId, String changedText) {
		
//		switch(resId){
//		case R.id.et_header:
//			listHeader.get(selectedRow).setMsg(changedText);
//			break;
//	
//		}
//		listHeader.get(selectedRow).setEdit(true);
//		mEditableListener.onEdited(mID);
	}

	@Override
	public void onCloseWithReturn(Class<? extends MoleQBaseDialog> pramClass, int viewId, String returnvalue) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onReturnValue(int what, Object obj) {
		// TODO Auto-generated method stub
		listHeader = (List<Receipt>) obj;
		mEditableListener.onEdited(mID);
	}

	@Override
	public void onShowingMessageDlg(boolean isShowing) {
		// TODO Auto-generated method stub

	}
}
